using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ShipkaDefenders.Gameplay
{
    public class HUD
    {
        protected AnaLogic g;
        public List<HUDButton> Buttons;
        public Vector2 ButtonSize = new Vector2(120, 95);
        public int SelectedUnitType = 0;

        public HUD(AnaLogic game)
        {
            g = game;
            Buttons = new List<HUDButton>() { };
        }
        public void Update(TimeSpan e)
        {
        }
        public bool Tap(Vector2 p)
        {
            bool hudtap = false;
            foreach (HUDButton hb in Buttons)
            {
                if (hb.Tap(p)) hudtap = true;
            }
            Rectangle click = new Rectangle((int)p.X, (int)p.Y, 1, 1);
            if (new Rectangle(10, 10, 66, 60).Intersects(click))
            {
                g.Engine.Status = (g.Engine.Status.Equals(GameStatus.Paused) ? GameStatus.Running : GameStatus.Paused);
            }
            else if (new Rectangle(76, 10, 80, 60).Intersects(click))
            {
                g.Engine.Status = (g.Engine.Status.Equals(GameStatus.FastForward) ? GameStatus.Running : GameStatus.FastForward);
            }
            return hudtap;
        }
        public void DrawSelf()
        {
            int k = 0;
            if (Buttons.Count > 0)
            {
                Texture2D t = g.MapTextures["side_bar"];
                g.spriteBatch.Draw(t, new Rectangle((g.GraphicsDevice.Viewport.Width - t.Width), 0, t.Width, g.GraphicsDevice.Viewport.Height), new Color(255, 255, 255, 255));
            }
            foreach (HUDButton hb in Buttons)
            {
                Vector2 p = new Vector2(((g.GraphicsDevice.Viewport.Width - ButtonSize.X)), (g.GraphicsDevice.Viewport.Height - ButtonSize.Y - (k * (ButtonSize.Y + 10)))) - new Vector2(10);
                hb.DrawRect = new Rectangle((int)p.X, (int)p.Y, (int)ButtonSize.X, (int)ButtonSize.Y);
                bool f = hb.UnitType.Equals(SelectedUnitType);
                if (hb.UnitType.Equals(0)) f = false;
                g.spriteBatch.Draw(g.MapTextures[hb.TextureName], hb.DrawRect, new Rectangle((int)(Convert.ToInt32(f) * ButtonSize.X), 0, (g.MapTextures[hb.TextureName].Width / 2), g.MapTextures[hb.TextureName].Height), Color.White);
                if (f && hb.UnitType > 0)
                {
                    g.spriteBatch.Draw(g.MapTextures["gui_navbar_build_title"], new Vector2(500, 10), Color.White);
                    g.spriteBatch.DrawString(g.MapFonts["Calibri18"], hb.Name, new Vector2(560, 24), Color.White);
                }
                k++;
            }
            g.spriteBatch.Draw(g.MapTextures["gui_navbar"], new Vector2(10), Color.White);
            g.spriteBatch.Draw(g.MapTextures["gui_ffw"], new Rectangle(90, 28, 50, 28), new Rectangle((Convert.ToInt32(g.Engine.Status.Equals(GameStatus.FastForward)) * (g.MapTextures["gui_ffw"].Width / 2)), 0, (g.MapTextures["gui_ffw"].Width / 2), g.MapTextures["gui_ffw"].Height), Color.White);
            g.spriteBatch.Draw(g.MapTextures["gui_pp"], new Rectangle(22, 20, 40, 40), new Rectangle((Convert.ToInt32(g.Engine.Status.Equals(GameStatus.Paused)) * (g.MapTextures["gui_pp"].Width / 2)), 0, (g.MapTextures["gui_pp"].Width / 2), g.MapTextures["gui_pp"].Height), Color.White);
            g.WriteString("Calibri", 18, g.Engine.Coins.ToString(), new Vector2(220, 24), Color.White);
            g.WriteString("Calibri", 18, g.Engine.Pass.ToString(), new Vector2(325, 24), Color.White);
            g.WriteString("Segoe", 16, string.Format("{0:00}:{1:00}", g.Engine.GetGameplayTime().Minutes, g.Engine.GetGameplayTime().Seconds), new Vector2(10, g.GraphicsDevice.Viewport.Height - 30), Color.White);
        }
        public void SelNone()
        {
            Clean();
        }
        public void SelBanner()
        {
            Clean();
            HUDButton hb;
            hb = new HUDButton("Bai Pesho", "new_russian");
            hb.UnitType = 1;
            hb.Tapped += delegate(object s, EventArgs e)
            {
                SelectedUnitType = 1;
                AutoState(EState.Banner_Anim);
            };
            Buttons.Add(hb);
            hb = new HUDButton("Gergi", "new_russian_sniper");
            hb.UnitType = 2;
            hb.Tapped += delegate(object s, EventArgs e)
            {
                SelectedUnitType = 2;
                AutoState(EState.Banner_Anim);
            };
            Buttons.Add(hb);
        }
        public void SelUnit()
        {
            Clean();
            HUDButton hb;
            hb = new HUDButton("Upgrade", "new_empty");
            hb.Tapped += delegate(object s, EventArgs e) { };
            Buttons.Add(hb);
        }
        // ---
        protected void Clean()
        {
            Buttons.Clear();
            SelectedUnitType = 0;
        }
        public void AutoState(EState s)
        {
            Ally sb = g.Engine.SelectedBanner();
            if (sb != null) sb.State = s;
        }
    }
    public class HUDButton
    {
        public string TextureName;
        public bool Selected = false;
        public string Name { get; set; }
        public int UnitType = 0;
        public Rectangle DrawRect { get; set; }

        public delegate void TappedEventHandler(object sender, EventArgs e);
        public event TappedEventHandler Tapped;
        protected virtual void OnTapped(EventArgs e)
        {
            if (Tapped != null) Tapped(this, e);
        }
        public HUDButton(string n, string t)
        {
            Name = n;
            TextureName = t;
        }
        public bool Tap(Vector2 p)
        {
            if (DrawRect.Intersects(new Rectangle((int)p.X, (int)p.Y, 1, 1)))
            {
                OnTapped(EventArgs.Empty);
                return true;
            }
            return false;
        }
    }
}
